import uicls
import uiutil
import uiconst
import uthread
import trinity
import blue

class OldSprite(uicls._BaseDecoCore, uicls._GetDecoMetaclassInst('triui.UISprite')):
    __guid__ = 'uicls.OldSprite'
    __persistvars__ = ['_dragging',
     '_dragAllowed',
     '_dragData',
     '_dragLastOver']
    default_name = 'sprite'
    default_noScale = 0
    default_texture = None
    default_texturePath = None
    default_color = None
    default_left = 0
    default_top = 0
    default_width = 64
    default_height = 64

    def ApplyAttributes(self, attributes):
        uicls._BaseDecoCore.ApplyAttributes(self, attributes)
        if attributes.__gotShape__:
            return 
        noScale = attributes.get('noScale', self.default_noScale)
        if noScale:
            self.noScale = noScale
        color = attributes.get('color', self.default_color)
        if (color is not None):
            self.SetRGB(*color)
        texturePath = attributes.get('texturePath', self.default_texturePath)
        if texturePath:
            self.LoadTexture(texturePath)
        else:
            texture = attributes.get('texture', self.default_texture)
            if (texture is not None):
                self.texture = texture
            else:
                self.texture = trinity.TriTexture()



    def LoadTexture(self, texturePath):
        if (self.texture and (texturePath == self.texture.pixels)):
            return 
        blue.resMan.SetUrgentResourceLoads(True)
        if not self.texture:
            self.texture = trinity.TriTexture()
        self.texture.pixels = texturePath
        self.rectLeft = 0
        self.rectTop = 0
        self.rectWidth = 0
        self.rectHeight = 0
        blue.resMan.SetUrgentResourceLoads(False)



    def FadeTo(self, alpha = 0.0):
        uthread.new(uicore.effect.CombineEffects, self, alpha=alpha).context = 'Sprite::FadeTo'



    def SetRGB(self, *color):
        self.color.SetRGB(*color)


    SetRGBA = SetRGB

    def GetRGB(self):
        return (self.color.r,
         self.color.g,
         self.color.b)



    def GetRGBA(self):
        return (self.color.r,
         self.color.g,
         self.color.b,
         self.color.a)



    def SetAlpha(self, alpha):
        self.color.a = alpha



    def GetAlpha(self):
        return self.color.a



    def IsDraggable(self):
        return hasattr(self, 'GetDragData')



    def IsBeingDragged(self):
        return getattr(self, '_dragging', False)



    def _OnMouseDown(self, *args):
        if (self.IsDraggable() and uicore.uilib.leftbtn):
            uicore.Message('DragDropGrab')
            self._dragMouseDown = (uicore.uilib.x, uicore.uilib.y)
            self._dragAllowed = True
        else:
            self._dragAllowed = False



    def _OnMouseUp(self, *args):
        return 



    def _OnMouseMove(self, *args):
        if self.IsBeingDragged():
            self.OnDragMove(self, self._dragData)
            where = uicore.uilib.mouseOver
            whereLast = getattr(self, '_dragLastOver', None)
            if (where is not self):
                if hasattr(where, 'OnDragMove'):
                    uthread.new(where.OnDragMove, self, self._dragData)
                if (whereLast is not where):
                    if hasattr(whereLast, 'OnDragExit'):
                        uthread.new(whereLast.OnDragExit, self, self._dragData)
                    if hasattr(where, 'OnDragEnter'):
                        uthread.new(where.OnDragEnter, self, self._dragData)
            self._dragLastOver = where
            return 
        if ((not self.IsDraggable()) or ((not uicore.uilib.leftbtn) or ((not self._dragAllowed) or (uicore.uilib.mouseTravel < 6)))):
            return 
        uthread.new(self.BeginDrag).context = 'Sprite::BeginDrag'



    def BeginDrag(self, *args):
        if (getattr(self, 'Draggable_blockDrag', 0) or (not getattr(self, '_dragAllowed', 0))):
            return 
        dragData = self.GetDragData(self)
        if not dragData:
            return 
        self._dragAllowed = False
        self._dragging = True
        self._dragData = dragData
        dragContainer = uicls.DragContainer(name='dragContainer', align=uiconst.ABSOLUTE, idx=0, pos=(0, 0, 32, 32), state=uiconst.UI_DISABLED, parent=uicore.layer.dragging)
        dragContainer.dragData = dragData
        self.KillClickThreads()
        self.DoDrag(dragContainer, dragData)



    def DoDrag(self, dragContainer, dragData):
        if dragContainer.dead:
            return 
        mouseOffset = self.PrepareDrag_(dragContainer, self)
        if self.destroyed:
            return 
        dragContainer.InitiateDrag(mouseOffset)
        if self.destroyed:
            uiutil.Flush(uicore.layer.dragging)
            return 
        dropLocation = uicore.uilib.mouseOver
        if self.IsBeingDragged():
            self._dragAllowed = False
            self._dragging = 0
            self._dragLastOver = None
            uiutil.Flush(uicore.layer.dragging)
            dropLocation = uicore.uilib.mouseOver
            if (getattr(dropLocation, 'OnDropData', None) and self.VerifyDrop(dropLocation, dragData)):
                uthread.new(dropLocation.OnDropData, self, dragData)
            else:
                self.OnDragCanceled_(self, dragData)
        self._dragData = None
        self.OnEndDrag_(self, dropLocation, dragData)



    def PrepareDrag_(self, dragContainer, dragSource, *args):
        return uiutil.PrepareDrag_Standard(dragContainer, dragSource)



    def OnDragCanceled_(self, dragSource, dragData, *args):
        return 



    def OnEndDrag_(self, dragSource, dropLocation, dragData, *args):
        return 



    def OnDragMove(self, dragSource, dragData, *args):
        return 



    def VerifyDrop(self, dragDestination, dragData, *args):
        return True




class Sprite(OldSprite):
    __guid__ = 'uicls.Sprite'


